<template>
  <div
    class="carousel-item-container"
    ref="container"
    @mousemove="handleMouseMove"
    @mouseleave="handleMouseLeave"
  >
    <div class="carousel-img" ref="image" :style="imagePosition">
      <ImageLoader
        @load="showWords"
        :src="carousel.bigImg"
        :placeholder="carousel.midImg"
      />
    </div>
    <div class="title" ref="title">{{ carousel.title }}</div>
    <div class="desc" ref="desc">{{ carousel.description }}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader";
// 改用了代理, 不再拼接url了
export default {
  components: {
    ImageLoader,
  },
  props: ["carousel", "isShowWords"],
  data() {
    return {
      titleWidth: 0,
      descWidth: 0,
      containerSize: null, // 容器的尺寸
      innerSize: null, // 背景图的尺寸
      mouseX: 0, // 鼠标的横坐标
      mouseY: 0,
    };
  },
  mounted() {
    // 加载dom后获取title和desc的宽度
    this.titleWidth = this.$refs.title.clientWidth; 
    this.descWidth = this.$refs.desc.clientWidth;
    // console.log(this.titleWidth, this.descWidth);

    this.setSize();
    this.mouseX = this.center.x;
    this.mouseY = this.center.y;
    window.addEventListener("resize", this.setSize);
  },
  destroyed() {
    window.removeEventListener("resize", this.setSize);
  },
  computed: {
    // 计算图片位置[图片坐标]
    imagePosition() {
      if (!this.innerSize || !this.containerSize) {
        return;
      }
      const extraWidth = this.innerSize.width - this.containerSize.width; // 图片超出的宽度
      const extraHeight = this.innerSize.height - this.containerSize.height;
      const left =
        "-" + (extraWidth / this.containerSize.width) * this.mouseX + "px";
      const top =
        "-" + (extraHeight / this.containerSize.height) * this.mouseY + "px";
      return {
        // left,
        // top,
        transform: `translate(${left}, ${top})`,
      };
    },
    center() {
      return {
        x: this.containerSize.width / 2,
        y: this.containerSize.height / 2,
      };
    },
  },
  methods: {
    // 显示文字
    showWords() {
      if (!this.isShowWords) {
        return;
      }
      // 标题
      const title = this.$refs.title;
      title.style.opacity = 1;
      title.style.width = 0;
      title.clientWidth; // 强制让浏览器渲染一次[reflow], 不这样做的话浏览器就会直接渲染下面设置的宽度, 就没有过渡动画效果了
      title.style.transition = "1s";
      title.style.width = this.titleWidth + "px";

      // 描述
      const desc = this.$refs.desc;
      desc.style.opacity = 1;
      desc.style.width = 0;
      desc.clientWidth; // 强制让浏览器渲染一次[reflow], 不这样做的话浏览器就会直接渲染下面设置的宽度, 就没有过渡动画效果了
      desc.style.transition = "2s 1.2s";
      desc.style.width = this.descWidth + "px";
    },
    // 设置各种尺寸
    setSize() {
      this.containerSize = {
        width: this.$refs.container.clientWidth,
        height: this.$refs.container.clientHeight,
      };
      this.innerSize = {
        width: this.$refs.image.clientWidth,
        height: this.$refs.image.clientHeight,
      };
      // console.log(this.containerSize, this.innerSize);
    },
    // 鼠标移入
    handleMouseMove(e) {
      const rect = this.$refs.container.getBoundingClientRect(); // 获取容器相对于视口的坐标
      this.mouseX = e.clientX - rect.left;
      this.mouseY = e.clientY - rect.top;
    },
    // 鼠标移出
    handleMouseLeave() {
      this.mouseX = this.center.x;
      this.mouseY = this.center.y;
    },
  },
};
</script>

<style lang="less" scoped>
@import "~@/styles/mixin.less";
@import "~@/styles/var.less";
.carousel-item-container {
  // background: @dark;
  width: 100%;
  height: 100%;
  color: #fff;
  position: relative;
  .carousel-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 110%;
    height: 110%;
    // z-index: -1; // 测试
    transition: 0.3s;
  }
  .title,
  .desc {
    position: absolute;
    left: 30px;
    color: #fff;
    letter-spacing: 3px; // 字间距
    opacity: 0;
    white-space: nowrap;
    overflow: hidden;
    text-shadow: 3px 2px 0 rgba(0, 0, 0, 0.5), 0 0 0 rgba(0, 0, 0, 0.5),
      0 1px 0 rgba(0, 0, 0, 0.5), 0 -1px 0 rgba(0, 0, 0, 0.5); // 文字阴影
  }
  .title {
    top: 45%;
    font-size: 2em;
  }
  .desc {
    margin-top: 40px;
    top: 48%;
    font-size: 1.2em;
    color: lighten(@gray, 20%);
  }
}
</style>
